package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.BeanPool;
import com.juyu.miliao.api.service.BeanPoolService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * @Author: ps
 * @Date: 2019/9/16 0016 下午 1:29
 */
@RestController
@RequestMapping("/beanPool")
public class BeanPoolController {
    @Resource
    private BeanPoolService beanPoolService;

    /**
     * 获取当前状态
     * @param token
     * @param userId
     * @param beanPool
     * @return
     */
    @GetMapping("/{userId:^[1-9][0-9]*$}")
    public BeanPool getBeanPoolByUserId(Authentication token, @PathVariable("userId") Long userId, BeanPool beanPool) {
        Subject subject = (Subject) token.getPrincipal();
        if (subject.getId() != userId)
            throw new IllegalParamException("用户id错误");
        beanPool.setUserId(subject.getId());
        beanPool.setCreateDate(LocalDate.now().minusDays(1));
        return beanPoolService.getBeanPool(beanPool);
    }

    /**
     * 领取寻豆
     * @param token
     * @param userId
     * @param beanPool
     * @return
     */
    @PatchMapping("/{userId:^[1-9][0-9]*$}")
    public void getBeansByUserId(Authentication token, @PathVariable("userId") Long userId, BeanPool beanPool) {
        Subject subject = (Subject) token.getPrincipal();
        if (subject.getId() != userId)
            throw new IllegalParamException("用户id错误");
        beanPool.setUserId(subject.getId());
        beanPool.setCreateDate(LocalDate.now().minusDays(1));
        beanPoolService.getBeansByUserId(beanPool);
    }

    /**
     * 测试
     * @param beanPool
     */
//    @PostMapping
//    public void update( BeanPool beanPool){
//        beanPoolService.updateBeanPoolBeans(beanPool);
//    }
}
